<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class WebHooksController extends Controller
{
    public function index(Request $request)
    {
        // 本地仓库路径
        $local = '/data/html/www/myBlog';

        // 安全验证字符串，为空则不验证
        $token = 'shuaiwang';


        // 如果仓库目录不存在，返回错误
        if (!is_dir($local)) {
            header('HTTP/1.1 500 Internal Server Error');
            die('Local directory is missing');
        }

        //如果请求体内容为空，返回错误
        $password = $request->input('password');
        if (!$password) {
            header('HTTP/1.1 400 Bad Request');
            die('HTTP HEADER or POST is missing.');
        }

        if ($token != $password) {
            header('HTTP/1.1 403 Permission Denied');
            die('Permission denied.');
        }

        echo shell_exec("sudo -i && cd {$local} && chmod -R 777 .git && git pull 2>&1");
        die("done " . date('Y-m-d H:i:s', time()));
    }
}
